package com.max.cms0.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.max.cms0.entity.User;
import com.max.cms0.entity.ext.userExtend;
import com.max.cms0.exception.ServiceException;
import com.max.cms0.result.ResultCode;
import com.max.cms0.service.UserService;
import com.max.cms0.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author lenovo
* @description 针对表【cms_user】的数据库操作Service实现
* @createDate 2024-11-18 14:40:07
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public User login(String username, String password) {
        //1.条件准备
        LambdaQueryWrapper<User> lqw = new
                LambdaQueryWrapper<User>();
        lqw.eq(User::getUsername, username)
                .eq(User::getPassword, password);
        //2.查询校验
        User user = userMapper.selectOne(lqw);
        if (user == null)
            throw new
                    ServiceException(ResultCode.USER_IS_NOT_EXIST);
        return user;
    }

    @Override
    public User queryById(Long id) {
        //1.有效参数判断
        if (id == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        //2.查找用户
        User user = userMapper.selectById(id);
        if (user == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        return user;
    }


    //分页+条件查询
    @Override
    public IPage<userExtend> query(Integer pageNum, Integer
            pageSize, String username, String status, Integer roleId,
                                   Integer isVip) {
        if (pageNum == null || pageSize == null)
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        //关键点：程序员自行书写sql语句，要完成多表连接查询，如何进行分页处理？ 推荐下面写法
        IPage<userExtend> page = new Page<>(pageNum, pageSize);
        userMapper.queryAllUserWithRole(page, username, status,
                roleId, isVip);
        //System.out.println(page.getTotal());
        //System.out.println(page.getRecords().size());
        return page;
    }

    @Override
    public boolean save(User user) {
        //1.判断username、password是否存在且不为空
        String username = user.getUsername();
        String password = user.getPassword();
        //1.1 null值判断
        if (username == null || password == null)
            throw new
                    ServiceException(ResultCode.PARAM_IS_BLANK);
        //1.2 空值判断 “”、“ ”、“ ”、“\t”
        username = username.trim();
        password = password.trim();
        if ("".equals(username) || "".equals(password))
            throw new
                    ServiceException(ResultCode.PARAM_IS_BLANK);
        //1.3 修改user中username、password值
        user.setUsername(username);
        user.setPassword(password);
        //2.判断username是否唯一
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>
                ();
        qw.eq(User::getUsername, username);
        User u = userMapper.selectOne(qw);
        if (u != null) {
            throw new
                    ServiceException(ResultCode.USERNAME_HAS_EXISTED);
        }
        //3.补充注册时间
        user.setRegisterTime(LocalDateTime.now());
        //4.新增用户
        userMapper.insert(user);
        return true;
    }
}




